import javax.media.opengl.*;

public class Square extends PhysicsObject {
  
  public Square(float size, boolean pickable) {
	  centerOfMass.x = centerOfMass.y = size / 2;
	  inverseMomentOfInertia = 1 / (float)(Math.pow(size, 4) / 12);
	  renderable = new Renderable(pickable);
	  renderable.scale = size;
	  renderable.CoMX = centerOfMass.x;
	  renderable.CoMY = centerOfMass.y;
	  inverseMass = 2 / size;
	  
	  Triangle t1 = new Triangle(size, false);
	  t1.centerOfMass = centerOfMass;
	  addChild(t1);
	  
	  Triangle t2 = new Triangle(size, false);
	  t2.orientationRelativeToParent = (float)Math.PI;
	  t2.centerOfMass = centerOfMass;
	  addChild(t2);
  }
  
  public Square (float size) {
	  this(size, true);
  }

	private class Renderable extends SceneGraphNode {
		public Renderable(boolean pickable) {
			super(pickable);
			physics = Square.this;
		}

		public void renderGeometry(GLAutoDrawable drawable) {
		    GL2 gl = drawable.getGL().getGL2();
		    new Triangle(scale).renderable.render(drawable);
		    gl.glTranslatef(1, 1, 0);
		    gl.glRotatef(180, 0, 0, 1);
		    new Triangle(scale).renderable.render(drawable);
		}
	}
	
	public static CollisionInfo getCollision(HalfSpace a, Square b) {
		CollisionInfo info1 = Triangle.getCollision((HalfSpace) a, (Triangle) b.children.get(0));
		CollisionInfo info2 = Triangle.getCollision((HalfSpace) a, (Triangle) b.children.get(1));
		if (info1 == null && info2 == null) return null;
		
		if (info1 != null && info2 != null) {
			if (info1.depth > info2.depth) return info1;
			else return info2;
		}
		
		if (info1 != null) return info1;
		else return info2;
	}
}
